package com.song.dao.impl;

import com.song.bean.Menu;
import com.song.dao.MenuDao;
import com.song.dao.Utils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuDaoImpl extends Utils implements MenuDao {
    /**
     * 获取菜单的信息,信息总条数
     * @return
     */
    @Override
    public List<Menu> getMenus(Integer pageIndex,Integer pageSize) {
        List<Menu> menuList = new ArrayList<>();

        try {
            List<Object> list = new ArrayList<>();
            StringBuffer buffer = new StringBuffer("select * from menu");
            if (pageIndex != null){
                buffer.append(" limit ?,?");
                list.add((pageIndex-1)*pageSize);
                list.add(pageSize);
            }
            resultSet = select(buffer.toString(), list);
            while (resultSet.next()) {
                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setFatherMenuId(resultSet.getInt("fathermenuid"));
                menu.setUrl(resultSet.getString("url"));
                menu.setMenuState(resultSet.getInt("menustate"));
                menu.setRemark(resultSet.getString("remark"));
                menuList.add(menu);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        System.out.println("*******"+menuList.size());
        return menuList;
    }
    /**
     * 通过菜单ID查询菜单信息
     * @param menuId 菜单ID
     * @return 菜单信息对象
     */
    @Override
    public Menu findById(Integer menuId) {
        Menu menu = new Menu();
        List<Object> list = new ArrayList<>();
        try {
            String sql = "select * from menu where menuid = ?";
            list.add(menuId);
            resultSet = select(sql, list);
            while (resultSet.next()) {
                menu.setMenuId(menuId);
                menu.setMenuName(resultSet.getString("menuName"));
                menu.setFatherMenuId(resultSet.getInt("fathermenuid"));
                menu.setUrl(resultSet.getString("url"));
                menu.setMenuState(resultSet.getInt("menustate"));
                menu.setRemark(resultSet.getString("remark"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return menu;
    }
    /**
     * 修改菜单信息
     * @param menu 修改的菜单对象
     * @return 被修改后的菜单对象
     */
    @Override
    public Boolean updateMenu(Menu menu) {
        List<Object> list = new ArrayList<>();
        try {
            String sql = "update menu set menuName=?,fatherMenuId=?,url=?,menuState=?,remark=? where menuId=?";
            list.add(menu.getMenuName());
            list.add(menu.getFatherMenuId());
            list.add(menu.getUrl());
            list.add(menu.getMenuState());
            list.add(menu.getRemark());
            list.add(menu.getMenuId());

            int update = update(sql, list);
            if (update > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return false;
    }
    /**
     * 新增菜单
     * @param menu 新增菜单信息
     * @return true 新增成功  false 失败
     */
    @Override
    public Boolean insertMenu(Menu menu) {
        try {
            List<Object> list = new ArrayList<>();
            String sql = "insert into menu values(null,?,?,?,?,?)";
            list.add(menu.getMenuName());
            list.add(menu.getFatherMenuId());
            list.add(menu.getUrl());
            list.add(menu.getMenuState());
            list.add(menu.getRemark());
            int update = update(sql, list);
            if (update > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return false;
    }
    /**
     * 删除菜单
     * @param menuId 要删除的菜单ID
     * @return true 成功  false 失败
     */
    @Override
    public Boolean deleteMenu(Integer menuId) {
        List<Object> list = new ArrayList<>();
        try {
            String sql = "delete from menu where menuId = ?";
            list.add(menuId);
            int update = update(sql, list);
            if (update > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return false;
    }
}
